from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import redirect
from django.core.urlresolvers import reverse

from common.models import Users,Goods,Types,Orders,Detail

# 公共信息加载
def loadinfo(request):
    '''公共信息加载'''
    context = {}
    lists = Types.objects.filter(pid=0)
    context['typelist'] = lists
    return context

# 我的订单
def viporders(request):
    '''当前用户订单'''
    context = loadinfo(request)
    # 获取当前用户的所有订单信息
    odlist = Orders.objects.filter(uid=request.session['vipuser']['id'])
    # 遍历当前用户的所有订单，添加他的订单详情
    for od in odlist:
        delist = Detail.objects.filter(orderid=od.id)
        # 遍历每个商品详情，从Goods中获取对应的图片
        for og in delist:
            og.picname = Goods.objects.only('picname').get(id=og.goodsid).picname
        od.detaillist = delist
    # 将整理好的订单信息放置到模板遍历中
    context['orderslist'] = odlist
    return render(request,"web/viporders.html",context)

def odstate(request):
    ''' 修改订单状态 '''
    try:
        oid = request.GET.get("oid",'0')
        ob = Orders.objects.get(id=oid)
        ob.state = request.GET['state']
        ob.save()
        return redirect(reverse('vip_orders'))
    except Exception as err:
        print(err)
        return HttpResponse("订单处理失败！")

def info(request):
    #context = loadinfo(request)
    # 获取当前用户的所有订单信息
    context = loadinfo(request)
    mod = Users.objects
    vipuser = mod.filter(id=request.session['vipuser']['id'])
    context['userslist'] = vipuser
    
    return render(request,"web/vipinfo.html",context)

def edit(request,uid):
    
    try:
        ob = Users.objects.get(id=uid)
        context = {'user':ob}
        return render(request,"web/vipedit.html",context)
    except Exception as err:
        print(err)
        context = {'info':'没有找到要修改的信息！'}
    return render(request,"web/vipedit.html",context)

def update(request,uid):
    try:
        ob = Users.objects.get(id=uid)
        ob.name = request.POST['name']
        ob.sex = request.POST['sex']
        ob.address = request.POST['address']
        ob.code = request.POST['code']
        ob.phone = request.POST['phone']
        ob.email = request.POST['email']
        ob.save()
        context = {'info':'修改成功！'}
        return render(request,"web/info.html",context)
    except Exception as err:
        print(err)
        context = {'info':'修改失败！'}
        return render(request,"web/info.html",context)

def resetps(request,uid):
    try:
        ob = Users.objects.get(id=uid)
        context = {'user':ob}
        return render(request,"web/vippassword.html",context)
    except Exception as err:
        print(err)
        context = {'info':'没有找到要修改的信息！'}
    return render(request,"web/info.html",context)

def doresetps(request,uid):
    try:
        ob = Users.objects.get(id=uid)
        #获取密码并md5
        import hashlib
        m = hashlib.md5()
        m.update(bytes(request.POST['password'],encoding="utf8"))
        ob.password = m.hexdigest()
        ob.save()
        context = {'info':'修改成功！'}
    except Exception as err:
        print(err)
        context = {'info':'修改失败！'}

    return render(request,"web/info.html",context)
